<!--
 * new page
 * @author: liuxin
 * @since: 2024-08-16
 * delete_admin.vue
-->
<template>
	<el-dialog v-model="dialogFormVisible" title='删除产品' width="30%" center>
		<span >请谨慎操作，删除后将失去此产品的记录</span>
		<template #footer>
			<span class="dialog-footer">
				<el-button type="primary" @click="deleteProductFn">确定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
	import { ref, onBeforeUnmount } from "vue"
	import { bus } from "@/utils/mitt.js"
	import {  deleteProduct } from '@/api/product.js'
	import { ElMessage } from 'element-plus'

	const productid = ref()
	bus.on('deleteProductId', (id : number) => {
		productid.value = id
	})
	const emit = defineEmits(['success'])
	// 删除产品
	const deleteProductFn = async () => {
			const res = await deleteProduct(productid.value)
			if (res.status == 0) {
				ElMessage({
					message: '删除产品成功',
					type: 'success'
				})
				emit('success')
				dialogFormVisible.value = false
			} else {
				ElMessage.error('删除产品失败')
				dialogFormVisible.value = false
			}
		}
		
	
	// 弹窗开关

	const dialogFormVisible = ref(false)
	// 打开编辑管理员弹窗
	const open = () => {
		dialogFormVisible.value = true
	}
	defineExpose({
		open
	})

	// 取消订阅/监听
	onBeforeUnmount(() => {
		bus.all.clear()
	})
</script>

<style scoped lang="scss"></style>